package lib;


import main.ErrorException;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.util.CellRangeAddress;

import define.Common;
import define.Config;
import define.ErrorDefine;

public class SheetInLib 
{
	private Sheet m_sheet;
	private MergeInLib[] m_mergeCells;
	private WorkbookInLib m_workbook;

	public SheetInLib(Sheet sheet, WorkbookInLib workbook)
	{
		m_sheet = sheet;
		m_workbook = workbook;
		m_mergeCells = new MergeInLib[m_sheet.getNumMergedRegions()];
		for(int i = 0; i < m_mergeCells.length; ++i)
		{
			m_mergeCells[i] = new MergeInLib(m_sheet.getMergedRegion(i));
		}
	}

	public String getName()
	{
		return m_sheet.getSheetName();
	}
	
	protected Sheet getRealSheet()
	{
		return m_sheet;
	}
	
	public MergeInLib isMergeCell(int row, int column)
	{
		for(MergeInLib merge: m_mergeCells)
		{
			if(merge.isMergeCell(row, column))
			{
				return merge;
			}
		}
		return null;
	}
	
	public int getRowNumber()
	{
		return m_sheet.getLastRowNum() + 1;
	}
	
	public int getNotEmptyRowNumber()
	{
		return m_sheet.getPhysicalNumberOfRows();
	}
	
	public RowInLib getRow(int index)
	{
		return new RowInLib(m_sheet.getRow(index));
	}
	
	private Cell getCell(int row, int column)
	{
		Row tempRow = m_sheet.getRow(row);
		if(tempRow == null)
		{
			return null;
		}
		Cell tempCell = tempRow.getCell(column);
		return tempCell;
	}
	
	public short getBackgroundColor(int row, int column)
	{
		Cell tempCell = getCell(row, column);
		if(tempCell == null)
		{
			return Config.NO_COLOR;
		}
		return tempCell.getCellStyle().getFillBackgroundColor();
	}
	
	public short getForegroundColor(int row, int column)
	{
		Cell tempCell = getCell(row, column);
		if(tempCell == null)
		{
			return Config.NO_COLOR;
		}
		return tempCell.getCellStyle().getFillForegroundColor();
	}
	
	public String getCellValue(int row, int column)
	{
		Cell tempCell = getCell(row, column);
		if(tempCell == null)
		{
			return "";
		}
		CellInLib cellInLib = new CellInLib(tempCell);
		return cellInLib.getValue();
	}
	
	public CellInLib getCellInLib(int row, int column)
	{
		return new CellInLib(getCell(row, column));
	}
	
	public RowInLib addRow(int index, int len, int position) throws ErrorException
	{
		if(m_sheet.getRow(index) != null)
		{
			throw new ErrorException(ErrorDefine.E_ROW_HAS_EXISTS_ERROR, index + "", position);
		}
		return new RowInLib(m_sheet.createRow(index), len);
	}
	
	public void setMerge(int x, int y, int width, int height)
	{
		m_sheet.addMergedRegion(new CellRangeAddress(y, y + height - 1, x, x + width - 1));
	}
	
	public void setXmlValue(int x, int y, String value, int position) throws ErrorException
	{
		value = Common.removeBlankAtEnd(value);
		setValue(x, y, value, position);
	}
	
	private void setValue(int x, int y, String value, int position) throws ErrorException
	{
		CellInLib cell = getCellInLib(y, x);
		if(cell.IsEmpty())
		{
			throw new ErrorException(ErrorDefine.E_CANT_FIND_CELL_ERROR, position);
		}
		cell.setValue(value);
	}
	
	public void setColor(int x, int y, short color, int position) throws ErrorException
	{
		CellInLib cell = getCellInLib(y, x);
		if(cell.IsEmpty())
		{
			throw new ErrorException(ErrorDefine.E_CANT_FIND_CELL_ERROR, position);
		}
		cell.setColor(color, m_workbook.createCellStyle());
	}
}
